window.onload = function () {
	let username = document.querySelector('#username')
	let password = document.querySelector('#password')
	let idCard = document.querySelector('#idCard')
	let errorName = document.querySelector('#errorName')
	let errorPwd = document.querySelector('#errorPwd')
	let errorId = document.querySelector('#errorId')
	let startBtn = document.querySelector('#startBtn')
	let myBtn = document.querySelector('#myBtn')
	let usernameValue
	let idCardValue
	let passwordValue
	username.addEventListener('blur', function () {
		let reg = new RegExp(/^[a-zA-Z0-9_-]{4,16}$/, 'g')
		if (reg.test(username.value) == true) {
			errorName.innerText = ''
			usernameValue = String(username.value)
		} else {
			errorName.innerText = '用户名不正确'
		}
	})
	password.addEventListener('blur', function () {
		let reg = new RegExp(/^1[3456789]\d{9}$/, 'g')
		if (reg.test(password.value) == true) {
			errorPwd.innerText = ''
			passwordValue = password.value
			console.log(typeof passwordValue)
		} else {
			errorPwd.innerText = '号码不正确'
		}
	})
	idCard.addEventListener('blur', function () {
		let reg = new RegExp(/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/, 'g')
		if (reg.test(idCard.value) == true) {
			errorId.innerText = ''
			idCardValue = idCard.value
		} else {
			errorId.innerText = '身份证不正确'
		}
	})
	startBtn.addEventListener('click', function () {
		if (
			usernameValue != null &&
			idCardValue != null &&
			passwordValue != null
		) {
			console.log(123)
			let url = 'http://newshopapi.0melon0.cn/api/f_user/register'
			let data = {
				name: usernameValue,
				phone: passwordValue,
				card: idCardValue,
			}
			fetch(url, {
				method: 'POST',
				headers: {
					'content-type': 'application/json',
				},
				body: JSON.stringify(data),
			})
				.then(function (response) {
					return response.json()
				})
				.then(function (data) {
					alert(data.msg)
					sessionStorage.setItem('token', data.result.token)
					sessionStorage.setItem('name', usernameValue)
					sessionStorage.setItem('phone', passwordValue)
					location.href = './预约页/index.html'
				})
				.catch(function (data) {
					alert(data.msg)
				})
		} else {
			alert('输入合法的正确名')
		}
	})
	myBtn.addEventListener('click', function () {
		location.href = './查看预约页/index.html'
	})
}
